home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / cbzone / c_graphics.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  16KB  |  509 lines

  1. #include "c_includes.h"
  2. /*
  3.  * cbzone_graphics.c
  4.  *  -- Todd W Mummert, December 1990, CMU
  5.  *
  6.  * RCS Info
  7.  *  $Header: c_graphics.c,v 1.1 91/01/12 02:03:33 mummert Locked $
  8.  */
  9.  
  10. Font controlfont, titlefont, generalfont;
  11.  
  12. void message(number, bell)
  13.      int number;
  14.      Bool bell;
  15. {
  16.   static int xpt = 70;
  17.   static int ypt[] = {507, 567, 627};
  18.   static char* messages[] = {"ENEMY TANK IN FIRING ARC",
  19.                                "MOVEMENT BLOCKED BY OBJECT",
  20.                                "SALVO FIRED BY ENEMY TANK"};
  21.   int ind;
  22.   char text[41];
  23.   
  24.   ind = abs(number) - 1;
  25.   if (number < 0) 
  26.     sprintf(text,"%-40s"," ");
  27.   else
  28.     sprintf(text,"%-40s",messages[ind]);
  29.  
  30.   gprsetclippingactive(False);
  31.   printstring(xpt, ypt[ind], text, 40);
  32.   gprsetclippingactive(True);
  33.   if (bell)
  34.     tonetime();
  35. }
  36.  
  37. void screeninit()
  38. {
  39.   controlfont = gprloadfontfile(CONTROLFONT);
  40.   titlefont = gprloadfontfile(TITLEFONT);
  41.   generalfont = gprloadfontfile(GENERALFONT);
  42.   staticscreen();
  43. }
  44.  
  45. void staticscreen()
  46. {
  47.   static int window[2][2] = {70, 75, 860, 350};
  48.   int pt[2], radius;
  49.   char text[30];
  50.   static short line[] = {58, 66, 66, 58, 56, 438, 68, 438, 938, 56,
  51.                            938, 68, 942, 442, 934, 434, 56, 472, 68, 472,
  52.                            934, 468, 942, 476, 58, 644, 66, 652, 932, 648,
  53.                            944, 648};
  54.   static short scan[] = {500, 495, 500, 500, 500, 625, 500, 620, 435, 560,
  55.                            440, 560, 565, 560, 560, 560};
  56.   static short scrn[] = {75, 64, 925, 64, 936, 75, 936, 425, 925, 436,
  57.                            75, 436, 64, 425, 64, 75, 75, 64};
  58.  
  59.   gprsetclippingactive(False);
  60.   gprsettextbackgroundvalue(opt->cpi[COLOR_BG]);
  61.   gprsetdrawvalue(opt->cpi[COLOR_FG]);
  62.   gprsettextvalue(opt->cpi[COLOR_TEXT]);
  63.   drawrectangle(50, 50, 900, 400);      /* draw the outer top rectangle */
  64.   drawrectangle(53, 53, 894, 394);      /*          inner top           */
  65.   drawrectangle(50, 460, 900, 200);     /*          outer bottom        */
  66.   drawrectangle(53, 463, 894, 194);     /*          inner bottom        */
  67.   polyline(scrn, 9);                    /*       inner top frame        */
  68.   pt[0] = 62;
  69.   pt[1] = 62;
  70.   radius = 6;
  71.   gprcircle(pt, radius);                /* now let's draw some screws   */
  72.   pt[0] = 938;
  73.   gprcircle(pt, radius);
  74.   pt[1] = 438;
  75.   gprcircle(pt, radius);
  76.   pt[0] = 62;
  77.   gprcircle(pt, radius);
  78.   pt[0] = 62;
  79.   pt[1] = 472;
  80.   gprcircle(pt, radius);
  81.   pt[0] = 938;
  82.   gprcircle(pt, radius);
  83.   pt[1] = 648;
  84.   gprcircle(pt, radius);
  85.   pt[0] = 62;
  86.   gprcircle(pt, radius);
  87.   multiline(line, 8);                   /*        now the slots         */
  88.   pt[0] = 500;
  89.   pt[1] = 560;
  90.   radius = 65;
  91.   gprsetdrawvalue(opt->cpi[COLOR_SCANNER]);
  92.   gprcircle(pt, radius);                /*      radar screen            */
  93.   radius = 64;
  94.   gprcircle(pt, radius);
  95.   multiline(scan, 4);
  96.   pt[0] = 855;
  97.   pt[1] = 560;
  98.   radius = 65;
  99.   gprsetdrawvalue(opt->cpi[COLOR_JOYSTICK]);
  100.   gprcircle(pt, radius);                /*      joystick screen         */
  101.   radius = 64;
  102.   gprcircle(pt, radius);
  103.   gprsetdrawvalue(opt->cpi[COLOR_TEXT]);
  104.   drawrectangle(590, 570, 55, 30);      /*      missile warning         */
  105.   drawrectangle(655, 570, 55, 30);      /*      lander warning          */
  106.   drawrectangle(590, 520, 120, 30);     /*         score box            */
  107.   gprsettextfont(titlefont);
  108.   strcpy(text, "BATTLEZONE V2.0c");
  109.   printstring(50, 45, text, strlen(text));
  110.   gprsettextvalue(opt->cpi[COLOR_JOYSTICK]);
  111.   strcpy(text, "F");                    /* directions for the joystick  */
  112.   printstring(850, 503, text, 1);
  113.   strcpy(text, "B");
  114.   printstring(850, 630, text, 1);
  115.   strcpy(text, "R");
  116.   printstring(913, 570, text, 1);
  117.   strcpy(text, "L");
  118.   printstring(785, 570, text, 1);
  119.   gprsettextvalue(opt->cpi[COLOR_TEXT]);
  120.   gprsettextfont(controlfont);
  121.   strcpy(text, "Missile");              /*      warning messages        */
  122.   printstring(595, 590, text, 7);
  123.   strcpy(text, "Lander");
  124.   printstring(664, 590, text, 6);
  125.   strcpy(text, "H");                    /*      switch positions        */
  126.   printstring(920, 608, text, 1);
  127.   strcpy(text, "L");
  128.   printstring(920, 642, text, 1);
  129.   gprsetclipwindow(window);             /* define the playing field     */
  130.   gprsettextfont(generalfont);
  131.   pt[0] = 500;
  132.   pt[1] = 355;
  133.   gprsetcursorposition(pt);
  134.   gprsetclippingactive(True);
  135. }
  136.  
  137. void updatedisplay (missile, lander, score, numleft, sens, reset)
  138.      Bool missile, lander, sens, reset;
  139.      int score, numleft;
  140. {
  141.   char text[11];
  142.   static Bool flasher[] = {False, False};
  143.   static int currentnumleft = 0;
  144.   static int currentscore = -1;
  145.   static int bswitch[] = {918, 612};
  146.   static int wind1[][2] = {591, 571, 54, 29};
  147.   static int wind2[][2] = {656, 571, 54, 29};
  148.   static int origin[][2] = {733, 493, 733, 533, 733, 573, 733, 613};
  149.   static Bool currentsens = True;
  150.  
  151.   gprsetclippingactive(False);
  152.   if (reset) {
  153.     flasher[0] = flasher[1] = False;
  154.     currentsens = True;
  155.   }
  156.   
  157.   if ((flasher[0] && !missile) ||       /* change the missile warning */
  158.       (!flasher[0] && missile)) {
  159.     flasher[0] = missile;
  160.     bitblt(wind1);
  161.   }
  162.   if ((flasher[1] && !lander) ||        /* change the lander warning */
  163.       (!flasher[1] && lander)) {
  164.     flasher[1] = lander;
  165.     bitblt(wind2);
  166.   }
  167.   if (score != currentscore) {          /*     change the score      */
  168.     currentscore = score;
  169.     sprintf(text,"%10d",score);
  170.     printstring(608, 542, text, strlen(text));
  171.   }
  172.   if (numleft < currentnumleft && numleft >= 0)    /* remove tank(s) */
  173.     while (numleft != currentnumleft)
  174.       removepixmap(4, origin[--currentnumleft]);
  175.   if (numleft > currentnumleft)                    /*  add tank(s)   */
  176.     while (numleft != currentnumleft && currentnumleft < 4)
  177.       putpixmap(4, origin[currentnumleft++]);
  178.   if (sens && !currentsens) {           /* change to high gear       */
  179.     putpixmap(2, bswitch);
  180.     currentsens = sens;
  181.   }
  182.   else if (!sens && currentsens) {      /* change to low gear        */
  183.     putpixmap(3, bswitch);
  184.     currentsens = sens;
  185.   }
  186.   gprsetclippingactive(True);
  187. }
  188.  
  189. void joystick (position, sens, pl)
  190.      int* position;
  191.      Genericp pl;
  192.      Bool sens;
  193. {
  194.   static float sscale[] = {0.06, 0.03};
  195.   static float rscale[] = {0.00013, 0.000065};
  196.   static int destorigin[] = {845, 550};
  197.   static short stick[8] = {0};
  198.   static Bool first = True;
  199.   static Bool draw = True;
  200.   int ind;
  201.   float scrot, sctot, scale, xl, yl, tl, xoff, yoff;
  202.  
  203.   if (sens)
  204.     ind = 0;
  205.   else
  206.     ind = 1;
  207.   pl->speed = -(position[1] - 355.0) * sscale[ind];
  208.   pl->rotate = (position[0] - 500.0) * rscale[ind];
  209.   if (fabs(pl->speed) > 10.0)
  210.     pl->speed = sign(10.0, pl->speed);
  211.   if (fabs(pl->rotate) > 0.030)
  212.     pl->rotate = sign(0.030, pl->rotate);
  213.   scrot = pl->rotate * 10.0 / 0.030;
  214.   sctot = sqrt(pl->speed*pl->speed + scrot*scrot);
  215.   if (sctot > 10.0) {
  216.     scale = 10.0 / sctot;
  217.     pl->speed *= scale;
  218.     pl->rotate *= scale;
  219.   }
  220.   if (draw) {
  221.     gprsetclippingactive(False);
  222.     if (!first) {
  223.       gprsetdrawvalue(opt->cpi[COLOR_BG]);
  224.       removepixmap(1, destorigin);
  225.       multiline(stick, 2);
  226.     }
  227.     else
  228.       first = False;
  229.     destorigin[0] =  35.0 * (pl->rotate / 0.03) + 845.0;
  230.     destorigin[1] = -35.0 * (pl->speed  / 10.0)  + 550.0;
  231.     putpixmap(1, destorigin);
  232.     xl = pl->rotate / 0.030;
  233.     yl = pl->speed / 10.0;
  234.     tl = sqrt(xl*xl + yl*yl);
  235.     if (tl > 0.05) {
  236.       yoff = xl / tl * 9.0;
  237.       xoff = yl / tl * 9.0;
  238.     }
  239.     else {
  240.       yoff = 0.0;
  241.       xoff = 0.0;
  242.     }
  243.     stick[0] = stick[4] = 855;
  244.     stick[2] = destorigin[0] + 10 + xoff / 2;
  245.     stick[6] = destorigin[0] + 10 - xoff / 2;
  246.     stick[1] = stick[5] = 560;
  247.     stick[3] = destorigin[1] + 10 + yoff / 2;;
  248.     stick[7] = destorigin[1] + 10 - yoff / 2;
  249.     gprsetdrawvalue(opt->cpi[COLOR_JOYSTICK]);
  250.     multiline(stick, 2);
  251.     gprsetclippingactive(True);
  252.   }
  253.   draw = !draw;
  254.   pl->rotate = -pl->rotate;
  255. }
  256.  
  257. /*
  258.  * scanner
  259.  *
  260.  * draws the radar scan...
  261.  */
  262. void scanner(o)
  263.      Genericp o;
  264. {
  265.   static float scale = 0.03;
  266.   static XSegment scanline[] = {500, 560, 500, 560};
  267.   static Bool init = True;
  268.   static XSegment* dot;
  269.   static float ca = 1.0;        /* keep track of where the scanner line */
  270.   static float sa = 0.0;        /* is currently positioned */
  271.   static float sda = 0.087156;
  272.   static float cda = 0.996195;
  273.   static int ind = 0;           /* number of enemies last on scanner */
  274.   float temp;
  275.   Genericp g;
  276.   XSegment* nextdot;
  277.   XSegment* curdot;
  278.  
  279.   if (init) {
  280.     dot = (XSegment*) malloc(sizeof(XSegment)*2*opt->mobjects);
  281.     if (dot == NULL) {
  282.       printf("Malloc failed...too many objects?\n");
  283.       exit(1);
  284.     }
  285.     init = False;
  286.   }
  287.  
  288.   gprsetclippingactive(False);
  289.   gprsetdrawvalue(opt->cpi[COLOR_BG]);
  290.   multiline(scanline, 1);               /*       erase scan line       */
  291.   if (ind)                              /* erase enemies, if necessary */
  292.     multiline(dot, ind);
  293.   temp = ca;
  294.   ca = ca * cda - sa * sda;
  295.   sa = sa * cda + sda * temp;
  296.   scanline->x2 = 60.0 * ca + 500;
  297.   scanline->y2 = 60.0 * sa + 560;
  298.   gprsetdrawvalue(opt->cpi[COLOR_SCANNER]);
  299.   multiline(scanline, 1);               /*     draw scanline           */
  300.   ind = 0;
  301.   curdot = dot;
  302.   for (g=o+opt->estart; g<o+opt->lstart; g++)
  303.     if (g->attr & IS_ALIVE && g->range <= 2000.0 && g->range != 0.0) {
  304.       nextdot = curdot + 1;
  305.       nextdot->x1 = curdot->x1 = 500 + g->prox * scale - 1;
  306.       nextdot->y2 = curdot->y1 = 560 - g->proy * scale + 1;
  307.       nextdot->x2 = curdot->x2 = curdot->x1 + 2;
  308.       nextdot->y1 = curdot->y2 = curdot->y1 - 2;
  309.       ind += 2;
  310.       curdot += 2;
  311.     }
  312.   if (ind)
  313.     multiline(dot, ind);                /* draw enemies, if necessary  */
  314.   gprsetclippingactive(True);
  315. }
  316.  
  317. void xhairs (aligned)
  318.      Bool aligned;
  319. {
  320.   static Bool last = False;
  321.   static short pt1[] = {500, 215, 500, 235, 500, 285, 500, 305, 475, 240,
  322.                           475, 235, 475, 235, 525, 235, 525, 235, 525, 240,
  323.                           475, 280, 475, 285, 475, 285, 525, 285, 525, 285,
  324.                           525, 280};
  325.   static short pt2[] = {500, 215, 500, 235, 500, 285, 500, 305, 475, 245,
  326.                           500, 235, 500, 235, 525, 245, 475, 275, 500, 285,
  327.                           500, 285, 525, 275};
  328.   static Bool draw = True;
  329.   if (draw) {
  330.     if (aligned) {
  331.       if (!last) {
  332.         gprsetdrawvalue(opt->cpi[COLOR_BG]);       /* erase the old */
  333.         multiline(pt1, 8);
  334.         last = True;
  335.       }
  336.       gprsetdrawvalue(opt->cpi[COLOR_XHAIR]);        /* draw the new */
  337.       multiline(pt2, 6);
  338.     }
  339.     else {
  340.       if (last) {
  341.         gprsetdrawvalue(opt->cpi[COLOR_BG]);       /* erase the old */
  342.         multiline(pt2, 6);
  343.         last = False;
  344.       }
  345.       gprsetdrawvalue(opt->cpi[COLOR_XHAIR]);        /* draw the new */
  346.       multiline(pt1, 8);
  347.     }
  348.   }
  349.   draw = !draw;
  350. }
  351.  
  352. void drawhorizon(azm)
  353.      float azm;
  354. {
  355.   static short horizon[] = {500, 240, 500, 240, 500, 240, 500, 240,
  356.                               500, 240, 500, 240, 500, 240, 500, 240,
  357.                               500, 240, 500, 240, 500, 240, 500, 240,
  358.                               500, 240, 500, 240, 500, 240, 500, 240,
  359.                               500, 240, 500, 240, 500, 240, 500, 240,
  360.                               500, 240, 500, 240, 500, 240, 500, 240,
  361.                               500, 240, 500, 240, 500, 240, 500, 240,
  362.                               500, 240, 500, 240, 500, 240, 500, 240,
  363.                               500, 240, 500, 240, 500, 240, 500, 240,
  364.                               500, 240, 500, 240, 500, 240, 500, 240,
  365.                               500, 240, 500, 240, 500, 240, 500, 240,
  366.                               500, 240, 500, 240, 500, 240, 500, 240};
  367.   static int number = 0;
  368.   static float rdc = 0.01745329251;
  369.   static float scale = 15.0;
  370.   static int numclust[] = {8, 12, 10, 12, 10, 10};
  371.   static int clustux[][12] = {150, 140, 150, 60, 85, -100, -50, -150, 0, 0,
  372.                                 0, 0, 150, 50, 75, 0, 0, 7, 0, -40, 0, -110,
  373.                                 -25, -150, 150, 115, 115, 105, 115, -10, 0,
  374.                                 -125, -85, -150, 0, 0, 150, 130, 150, 10, 25,
  375.                                 -35, -35, 0, -35, -140, -110, -150, 150, -10,
  376.                                 25, -70, -50, -125, -125, -100, -125, -150,
  377.                                 0, 0, 150, 100, 140, 0, 0, -30, 0, -120, -60,
  378.                                 -150, 0, 0};
  379.   static int clustuy[][12] = {160, 220, 160, 250, 225, 245, 250, 240, 0, 0, 0,
  380.                                 0, 240, 227, 245, 190, 190, 215, 190, 225, 190,
  381.                                 213, 240, 200, 200, 189, 189, 225, 189, 244,
  382.                                 250, 165, 213, 135, 0, 0, 135, 200, 135, 210,
  383.                                 225, 165, 165, 230, 165, 228, 210, 215, 215,
  384.                                 217, 230, 195, 210, 150, 150, 210, 150, 173,
  385.                                 0, 0, 173, 209, 225, 170, 170, 225, 170, 230,
  386.                                 200, 160, 0, 0};
  387.   static int pt[2];
  388.   static Bool moon = False;
  389.  
  390.   float center, xoff;
  391.   float deg, degmin, degmax;
  392.   int indmin, indmax, i, i1, j;
  393.   Bool flag1, flag2;
  394.   short* current;
  395.   
  396.   gprsetdrawvalue(opt->cpi[COLOR_BG]);
  397.   multiline(horizon, number);           /* erase horizon */
  398.   if (moon)
  399.     removepixmap(0, pt);                /* erase moon    */
  400.   gprsetdrawvalue(opt->cpi[COLOR_HORIZON]);
  401.   deg = azm / rdc;
  402.   if (deg >= 360.0)
  403.     deg -= 360.0;
  404.   degmin = deg - 30.0;
  405.   degmax = deg + 30.0;
  406.   flag1 = False;
  407.   flag2 = False;
  408.   if (degmin < 0.0) {
  409.     degmin += 360.0;
  410.     flag1 = True;
  411.   }
  412.   if (degmax >= 360.0) {
  413.     degmax -= 360.0;
  414.     flag2 = True;
  415.   }
  416.   indmin = (int) (degmin / 20.0);
  417.   indmax = (int) (degmax / 20.0) + 1;
  418.   number = 0;
  419.   current = horizon;
  420.   if (!flag1 && !flag2)
  421.     for (i=indmin; i<indmax; i++) {
  422.       center = 10.0 + i * 20.0;
  423.       xoff = (deg - center) * scale + 500.0;
  424.       i1 = i % 6;
  425.       for (j= 0; j<numclust[i1]; j++){
  426.         *current++ = clustux[i1][j] + xoff;
  427.         *current++ = clustuy[i1][j];
  428.         number++;
  429.       }
  430.     }
  431.   else if (flag1) {
  432.     for (i= indmin; i<18; i++) {
  433.       center = 10.0 + i * 20.0;
  434.       xoff = (360.0 + deg - center) * scale + 500.0;
  435.       i1 = i % 6;
  436.       for (j=0; j<numclust[i1]; j++) {
  437.         *current++ = clustux[i1][j] + xoff;
  438.         *current++ = clustuy[i1][j];
  439.         number++;
  440.       }
  441.     }
  442.     for (i=0; i<indmax; i++) {
  443.       center = 10.0 + i * 20.0;
  444.       xoff = (deg - center) * scale + 500.0;
  445.       i1 = i % 6;
  446.       for (j=0; j<numclust[i1]; j++) {
  447.         *current++ = clustux[i1][j] + xoff;
  448.         *current++ = clustuy[i1][j];
  449.         number++;
  450.       }
  451.     }
  452.   }
  453.   else {
  454.     for (i=indmin; i<18; i++) {
  455.       center = 10.0 + i * 20.0;
  456.       xoff = (deg - center) * scale + 500.0;
  457.       i1 = i % 6;
  458.       for (j=0; j<numclust[i1]; j++) {
  459.         *current++ = clustux[i1][j] + xoff;
  460.         *current++ = clustuy[i1][j];
  461.         number++;
  462.       }
  463.     }
  464.     for (i=0; i<indmax; i++) {
  465.       center = 10.0 + i * 20.0;
  466.       xoff = (deg - 360.0 - center) * scale + 500.0;
  467.       i1 = i % 6;
  468.       for (j=0; j<numclust[i1]; j++) {
  469.         *current++ = clustux[i1][j] + xoff;
  470.         *current++ = clustuy[i1][j];
  471.         number++;
  472.       }
  473.     }
  474.   }
  475.   number >>= 1;
  476.   multiline(horizon, number);           /* draw horizon */
  477.   if (deg <= 360.0 && deg >= 270.0) {
  478.     moon = True;
  479.     pt[0] = 500 + (deg - 315.0) * scale - 20;
  480.     pt[1] = 105 - 20;
  481.     putpixmap(0, pt);                   /* draw moon    */
  482.   }
  483.   else
  484.     moon = False;
  485. }
  486.  
  487. void drawcracks()
  488. {
  489.   static short pt[] = {190, 75, 270, 175, 270, 175, 230, 265, 240, 243,
  490.                          330, 290, 310, 280, 390, 235, 370, 246, 380, 195,
  491.                          200, 425, 290, 365, 270, 378, 300, 325, 630, 425,
  492.                          560, 335, 580, 361, 626, 295, 570, 348, 510, 355,
  493.                          520, 354, 490, 275, 500, 301, 440, 290, 810, 75,
  494.                          720, 125, 720, 125, 695, 205, 700, 200, 590, 210,
  495.                          630, 206, 620, 256, 710, 157, 780, 245, 770, 232,
  496.                          730, 305, 740, 287, 760, 315};
  497.  
  498.   gprsetdrawvalue(opt->cpi[COLOR_CRACKS]);
  499.   multiline(pt, 19);                    /* draw cracks */
  500. }
  501.  
  502. void clearscreen ()
  503. {
  504.   static int destorigin[] = {70, 75};
  505.   static int window[][2] = {70, 75, 860, 350};
  506.  
  507.   clearrectangle(window, destorigin);        /* erase the main viewscreen */
  508. }
  509.